package com.zhuge_new.Activity;

import android.arch.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Adapter.CheckAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.CheckBean;
import com.zhuge_new.bean.ListDetailsBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.DataUtilS;
import com.zhuge_new.untils.DateUtil;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.Calendar;

import javax.microedition.khronos.opengles.GL;

/**
 * Created by lenovo on 2018-09-18.
 */

public class CheckActivity extends BaseActivity implements View.OnClickListener {

    private View head;
    private RecyclerView check_list;
    private String bank;
    private String name;
    private String logoUrl;
    private String creditcard;
    private String creditcardId;
    private TextView tv_title;
    private ImageView check_logo;
    private TextView check_balk;
    private TextView check_number;
    private LinearLayout check_refesh;
    private TextView check_last;
    private TextView check_money;
    private TextView check_compile;
    private TextView check_repay;
    private TextView all_plan;
    private String mon;
    private CheckBean checkBean;
    private ListDetailsBean listDetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_activity);
        head = View.inflate(baseContext, R.layout.check_head, null);
        Intent intent = getIntent();
        creditcardId = intent.getStringExtra("creditcardId");
        creditcard = intent.getStringExtra("creditcard");
        init_title();
        getData();
        getList();
    }

    @Override
    public void init_title() {
        super.init_title();
        tv_title = findViewById(R.id.tv_nav_title);
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        check_logo = head.findViewById(R.id.check_logo);
        check_balk = head.findViewById(R.id.check_balk);
        check_number = head.findViewById(R.id.check_number);
        check_refesh = head.findViewById(R.id.check_refesh);
        check_last = head.findViewById(R.id.check_last);
        check_money = head.findViewById(R.id.check_money);
        check_compile = head.findViewById(R.id.check_compile);
        check_repay = head.findViewById(R.id.check_repay);
        all_plan = head.findViewById(R.id.all_plan);
        check_list = findViewById(R.id.check_list);
        check_list.setLayoutManager(new LinearLayoutManager(this));
        iv_back.setOnClickListener(this);
        check_compile.setOnClickListener(this);
        all_plan.setOnClickListener(this);
        CheckAssignment();
    }

    private void CheckAssignment() {
        tv_title.setText("账单");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(CheckActivity.class);
                break;
            case R.id.check_compile:
                Intent intent = new Intent(CheckActivity.this, CompileActivity.class);
                intent.putExtra("bank", checkBean.getCreditcard().getBank());
                intent.putExtra("creditcard", creditcard);
                intent.putExtra("creditcardId", creditcardId);
                startActivity(intent);
                break;
            case R.id.all_plan:
                Intent intent1 = new Intent(CheckActivity.this, AllplanActivity.class);
                intent1.putExtra("cardId", creditcardId);
                startActivity(intent1);
        }
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.creditcard_details)
                .tag(CheckActivity.this)
                .headers("token", PreferencesUtils.getString(this, "token"))
                .params("creditcardId", creditcardId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        checkBean = (CheckBean) new DataParse().getBean(response.body(), CheckBean.class, 0);
                        check_balk.setText(checkBean.getCreditcard().getBank());
                        check_number.setText(checkBean.getCreditcard().getCreditcard().substring(checkBean.getCreditcard().getCreditcard().length() - 4, checkBean.getCreditcard().getCreditcard().length()));
                        String url=checkBean.getLogoUrl();
                        Glide.with(baseContext).load(url).into(check_logo);
                        check_money.setText(checkBean.getCreditcard().getBillamount());
                        int tear = DataUtilS.getYear();
                        int month = DataUtilS.getMonth();
                        int sky = DataUtilS.getCurrentDayOfMonth();
                        int DaysOfMonth = DataUtilS.getDaysOfMonth(tear, month);
                        if (month < 10) {
                            mon = "0" + String.valueOf(month);
                        } else {
                            mon = String.valueOf(month);
                        }


                        if (sky < Integer.parseInt(checkBean.getCreditcard().getRepaymentDay())) {
                            check_last.setText("最后还款日" + mon + "/" + checkBean.getCreditcard().getRepaymentDay());
                        } else {
                            int months = Integer.parseInt(mon);
                            months = months + 1;
                            check_last.setText("最后还款日" + months + "/" + checkBean.getCreditcard().getRepaymentDay());
                        }
                    }
                });
    }

    public void getList() {
        /*Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        String selechmonth = year + "-" + month;*/
        OkGo.<String>post(BaseHttp.get_Payrecord)
                .tag(this)
                .headers("token", PreferencesUtils.getString(this, "token"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body());
                            String payrecords = obj.getString("payrecords");
                            if (payrecords.isEmpty()) {
                                listDetailsBean = new ListDetailsBean();
                                listDetailsBean.setMsgcode(100);
                            } else {
                                listDetailsBean = (ListDetailsBean) new DataParse().getBean(response.body(), ListDetailsBean.class, 0);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        CheckAdapter checkAdapter = new CheckAdapter(CheckActivity.this, listDetailsBean, creditcard);
                        checkAdapter.addHeaderView(head);
                        checkAdapter.setHeaderFooterEmpty(true, false);
                        check_list.setAdapter(checkAdapter);
                        checkAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.check_entry_ety:
                                        Intent intent = new Intent(CheckActivity.this, TransactionActivity.class);
                                        intent.putExtra("payType", listDetailsBean.getPayrecords().get(position).getPayType());
                                        intent.putExtra("paySum", listDetailsBean.getPayrecords().get(position).getPaySum());
                                        intent.putExtra("realSum", listDetailsBean.getPayrecords().get(position).getRealSum());
                                        intent.putExtra("payTime", listDetailsBean.getPayrecords().get(position).getPayTime());
                                        intent.putExtra("cardNo", listDetailsBean.getPayrecords().get(position).getCardNo());
                                        intent.putExtra("rate", listDetailsBean.getPayrecords().get(position).getRate());
                                        startActivity(intent);
                                        break;
                                }
                            }
                        });
                    }
                });


    }
}
